<?php
/** Rho form generation module.
*
* This file is normally loaded on-demand by the Rho_Form class.
*
* @see Rho_Form
* @package Rho
* @subpackage Rho-forms 
* @author Pete
* @version 0.1
* @license http://www.opensource.org/licenses/apache2.0.php Apache License, Version 2.0.*/

/** Provides  static methods for dynamically generating a HTML form.
*
* @see Rho_Form
* @package Rho
* @subpackage Rho-forms */
class RForm_Generator{

  public static function generate(& $rules, & $renderers, & $data, & $id, & $method,
   & $action, & $enctype, array &  $attributes=NULL){
    echo '<form id="'.$id.'" method="'.$method.'" action="'.$action.'" 
    name="'.$id.'" enctype="'.$enctype.'"';
    if($extra!=NULL ) echo $extra;
    echo ">\n";
    foreach($renderers as $key=>$renderer):
     $r_id=$id.'_'.$key; 
     if($renderer->label() ){
       echo '  <label for="'.$r_id.'" >label</label>'."\n";
     }
     echo '  ';
     $renderer->render($key, $r_id, $data[$key]);
     echo "\n";
    endforeach;
    echo "</form>\n";
  }
  
  protected static function startItem(){
    echo '  <p>';
  }
  
  protected static function endItem(){
    echo '</p>'."\n";
  }

}

/** Interface for Form Components.
*
* @see Rho_Form
* @package Rho
* @subpackage Rho-forms */
interface RForm_Widget{

  function __construct(array $attributes=NULL );
  
  function label();
  
  function render(& $name, & $id, & $value);

}

/*
* @package Rho
* @subpackage Rho-forms */
abstract class RForm_Base_Widget implements RForm_Widget{
  
  protected $attributes;  
  
  function __construct(array $attributes=NULL){
    $this->_attributes=$attributes;  
  }
  
  protected function openTag($type, & $name, & $id, & $value){
    echo '<input type="'.$type.'" name="'.$name.'" id="'.$id.'" value="'.$value.'" ';
    if($this->attributes!=NULL ){
      foreach($this->attributes as $attr=>$attr_value) :
        echo $attr.'="'.$attr_value.'" ';
      endforeach;
    } 
  }
  
  protected function closeTag(){
    echo ' />';
  }
  
  public function label(){
    return TRUE;
  }


}

/*
* @package Rho
* @subpackage Rho-forms */
class RForm_Text extends RForm_Base_Widget implements RForm_Widget{
  
  public function __construct(array $attributes=NULL ){
    parent::__construct($attributes);
    if($this->attributes==NULL){
      $this->attributes=array('size'=>32, 'maxlength'=>64);
    }
    else {
      $this->attributes['size']=Rho::ifset($this->attributes, 'size', 32);
      $this->attributes['maxlength']=Rho::ifset($this->attributes, 'maxlength', 64);
    }
  }
  
  public function render(& $name, & $id, & $value){
    parent::openTag('text',  $name, $id, $value);
    parent::closeTag();
  }

}

/*
* @package Rho
* @subpackage Rho-forms */
class RForm_Hidden extends RForm_Base_Widget implements RForm_Widget{
  
  function __construct(array $attibutes=NULL ){
    $this->attributes=NULL;
  }
  
  function render(& $name, & $id, & $value){
    parent::openTag('hidden',  $name, $id, $value);
    parent::closeTag();
  }
  
  public function label(){
    return FALSE;
  }
}